// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import ComponentBase from "../component/ComponentBase";
import DataMgr from "../mgr/dataManager";
import GameMgr from "../mgr/gameManager";
import NativeMgr from "../mgr/nativeManager";
import NetMgr from "../mgr/netManager";



const { ccclass, property } = cc._decorator;



@ccclass
export default class MsgLayer extends ComponentBase {


    closeAll: cc.Node = null;
    items: cc.Node = null;
    runNode: cc.Node = null;
    getNode: cc.Node = null;
    show: cc.Node = null;
    pool: cc.Node = null;
    curIndex: number = 0;
    getCoin: number = 0;
    configs: any = [
        { id: 1, coin: 100, weight: [6000, 6000] },
        { id: 3, coin: 200, weight: [3500, 2500] },
        { id: 5, coin: 500, weight: [800, 800] },
        { id: 7, coin: 1000, weight: [400, 350] },
        { id: 9, coin: 2000, weight: [200, 180] },
        { id: 11, coin: 3000, weight: [120, 100] },
        { id: 2, percent: 1, weight: [400, 2000] },
        { id: 4, percent: 2, weight: [200, 900] },
        { id: 6, percent: 3, weight: [120, 500] },
        { id: 8, percent: 5, weight: [80, 300] },
        { id: 10, percent: 8, weight: [50, 150] },
        { id: 12, percent: 10, weight: [40, 100] },


    ];
    onLoad(): void {
        super.onLoad();

        this.scheduleOnce(() => {
            this.closeAll.active = true;
        }, 5);

        this.initEvent()
        NativeMgr.Ins().showFeedAds();


        this.initPrizeInfo()


    }

    initEvent() {
        cc.systemEvent.on("getEcpm", this.onAdClose.bind(this), this);
    }

    onAdClose() {

    }

    initLeftTimes() {

    }
    initPrizeInfo() {
        this.runNode.active = true;
        this.getNode.active = false;
        this.show.active = false;
        this.initItems();
        this.initItemInfo()

        const leftTimesNode = this.runNode.getChildByName('leftTimes');
        leftTimesNode.getComponent(cc.Label).string = `剩余${DataMgr.Ins().userInfo.rolling}次`;

        this.pool.active = true;
        this.pool.getComponent(cc.Label).string = `${DataMgr.Ins().rollingPool}`;

    }


    initGet(coin) {
        this.runNode.active = false;
        this.getNode.active = true;
        this.show.getComponent(cc.Label).string = `${coin}金币`;
    }
    initItems() {
        for (let i in this.items.children) {
            const node = this.items.children[i];
            const bg = node.getChildByName("light")
            bg.active = false;
            const info = node.getChildByName('info');
        }
    }

    initItemInfo() {
        for (let i in this.configs) {
            const config = this.configs[i];
            const id = config.id;
            const coin = config.coin;
            const percent = config.percent;
            if (coin) {
                const info = this.items.getChildByName('item_' + (id - 1));
                info.getChildByName('info').getComponent(cc.Label).string = `${coin}\n金币`;
            } else {
                const info = this.items.getChildByName('item_' + (id - 1));
                info.getChildByName('info').getComponent(cc.Label).string = `${percent}% \n奖池`;
            }
        }
    }
    closeAllCallBack() {
        NativeMgr.Ins().delFeedAds();
        this.node.removeFromParent(true);
        this.node.destroy();
        NativeMgr.Ins().showExpressAds(0);
    }

    async runBtnCallBack() {
        // this.runLightAction(5);
        // this.getCoin = 2000;
        // // DataMgr.Ins().userInfo.coin = data.user.coin;
        // this.runLightAction(6);


        NativeMgr.Ins().showAds(2);


        const infoTotal = await NetMgr.Ins().servo.get(`/games/rolling/rdm`);

        if (infoTotal.status == 200) {
            const data = infoTotal.body;

            console.log('rolling data = ', data);
            // this.runLightAction(5);
            if (data.status == 'ok') {
                DataMgr.Ins().userInfo.rolling--;
                this.initLeftTimes();
                const id = data.data.item.id;
                const coin = data.data.item.coin;
                this.getCoin = coin;
                DataMgr.Ins().userInfo.coin = data.data.user.coin;
                this.runLightAction(id - 1);

            } else {
                GameMgr.Ins().showMsg('/games/rolling/rdm' + data.status);
            }

        } else {
            GameMgr.Ins().showMsg('/games/rolling/rdm' + infoTotal.status);
        }

    }


    runLightAction(toIndex: number) {
        this.runNode.active = false;
        const starSpeed = 0.5;

        const norSpeed = 0.1;
        const addSpeed = -0.1;
        const defSpeed = 0.12;

        let step = this.curIndex > toIndex ? this.curIndex - toIndex : toIndex - this.curIndex;
        // console.log('step1 = ', step);
        if (step < 36) {
            step += 36;
        }

        // console.log('step2 = ', step);

        let delayTime = 0;
        let runTimes = 0;
        for (let i = 0; i < step; i++) {

            let delTime = starSpeed + addSpeed * (i) > norSpeed ? starSpeed + addSpeed * (i) : norSpeed;
            if (i > step - 5) {
                delTime = norSpeed + defSpeed * (i + 5 - step);
            }
            // console.log('delTime = ', delTime);
            this.node.runAction(cc.sequence(cc.delayTime(delayTime), cc.callFunc(() => {

                const node = this.items.getChildByName('item_' + this.curIndex);
                const bg = node.getChildByName("light");
                bg.active = true;
            }), cc.delayTime(delTime), cc.callFunc(() => {
                const node = this.items.getChildByName('item_' + this.curIndex);
                const bg = node.getChildByName("light");
                bg.active = false;
                this.curIndex++;
                if (this.curIndex > 11) {
                    this.curIndex = 0;
                }
                runTimes++;
                if (runTimes >= step) {
                    this.runOverAction()
                }
            })))

            delayTime += delTime + 0.01;
        }
    }

    runOverAction() {
        console.log('this.curIndex = ', this.curIndex);
        const light = this.items.getChildByName('item_' + this.curIndex);
        const bg = light.getChildByName("light");
        bg.active = true;
        bg.runAction(cc.sequence(cc.fadeOut(0.15), cc.fadeIn(0.15)).repeat(3));
        this.getNode.active = true;
        this.show.active = true;
        this.show.getComponent(cc.Label).string = `${this.getCoin}金币`;
        this.pool.active = false;
        this.node.runAction(cc.sequence(cc.delayTime(0.9), cc.callFunc(() => {
            this.show.runAction(cc.sequence(cc.scaleTo(0.15, 1.2), cc.scaleTo(0.15, 1)).repeat(2));
        })))

    }


    getBtnCallBack() {
        NativeMgr.Ins().delFeedAds();
        this.node.removeFromParent(true);
        this.node.destroy();
        NativeMgr.Ins().showExpressAds(0);
    }





    onDestroy(): void {
        super.onDestroy();
        cc.systemEvent.targetOff(this);

    }




    // update (dt) {}
}
